from functools import wraps

from django.http import JsonResponse
from django.shortcuts import render, redirect
from django.urls import reverse

from App_ReadNovel.Tool import TranslateTool, isnice, iscollection
from App_ReadNovel.models import bookmark, wordlist
# Create your views here.
from App_SearchNovel.Tools import ParseBook, isLogin
from App_SearchNovel.models import Books
from App_User_SigninAndRegister.models import User
from NiceNovel.tool import forlogin




# 写书签
def writebookmark(request):
    status = request.session.get("islogin", False)
    mes = {"status": status}
    if status:
        request.GET.get("mark")
        bookid = request.GET.get("bookid")
        userid = request.session.get("userid")
        u = User.objects.get(userid=userid)
        u.bookmark.add(bookmark.objects.create(bookid=bookid, data=request.GET.get("mark")))
        mes["info"] = "操作成功"
        return JsonResponse(mes)
    else:
        return JsonResponse(mes)


# 翻译
@forlogin()
def translateword(request):
    status = request.session.get("vipstatus")
    if status =="1":
        word = request.GET.get("word")
        bookid = request.GET.get("bookid")
        try:
            mean = TranslateTool(word)
            userid = request.session.get("userid")
            u = User.objects.get(userid=userid)
            w = wordlist.objects.create(word=word, mean=mean, bookid=bookid)
            u.wordlist.add(w)
        except:
            mean = "没有查找到数据，该功能目前只允许查找单词，不能选中空格"
            word = "无"
        mes = {"word": word, "mean": mean,"vip":True,"status":True}
        return JsonResponse(mes)
    else:
        mes = {"vip":False,"status":True}
        return JsonResponse(mes)


# 点赞
def nice(request):
    status = request.session.get("islogin", False)
    mes = {"status": status}
    if status:
        uid = request.session.get("userid")
        bookid = request.GET.get("bookid")
        print(bookid)
        if not isnice(uid, bookid):
            book = Books.objects.get(bookid=bookid)
            book.nice = book.nice + 1
            book.isnice.add(User.objects.get(userid=request.session.get("userid")))
            book.save()
            mes["data"] = "已赞"
            return JsonResponse(mes)
        else:
            book = Books.objects.get(bookid=bookid)
            book.nice = book.nice - 1
            book.isnice.remove(User.objects.get(userid=request.session.get("userid")))
            book.save()
            mes["data"] = "点赞"
            return JsonResponse(mes)
    else:
        return JsonResponse(mes)


# 收藏
def collectionbook(request):
    status = request.session.get("islogin", False)
    mes = {"status": status}
    if status:
        userid = request.session.get("userid")
        bookid = request.GET.get("bookid")
        if not iscollection(userid, bookid):
            u = User.objects.get(userid=userid)
            b = Books.objects.get(bookid=bookid)
            b.iscollection.add(u)
            mes["info"] = "已收藏"
            return JsonResponse(mes)
        else:
            b = Books.objects.filter(bookid=bookid)[0]
            u = User.objects.filter(userid=userid)[0]
            b.iscollection.remove(u)
            mes["info"] = "收藏"
            return JsonResponse(mes)
    else:
        return JsonResponse(mes)
